package com.bw.title.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.bw.title.mapper.TbHouseMessageMapper;
import com.bw.po.TbHouseMessage;
import com.bw.result.ResultVo;

import com.bw.title.service.TbHouseMessageService;

import com.bw.vo.IdVo;
import com.bw.vo.TbMessageVo;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;



/**
* @author lenovo
* @description 针对表【tb_house_message(资讯表)】的数据库操作Service实现
* @createDate 2023-11-10 16:48:40
*/
@Service
public class TbHouseMessageServiceImpl extends ServiceImpl<TbHouseMessageMapper, TbHouseMessage>
    implements TbHouseMessageService {
    @Autowired
    private TbHouseMessageMapper tbHouseMessageMapper;


    @Override
    public ResultVo houseMessage(TbMessageVo tbMessageVo) {
//        //使用LambdaQueryWrapper存储信息
//        LambdaQueryWrapper<TbHouseMessage> queryWrapper = new LambdaQueryWrapper<>();
//       //查询条件
//        //判断标题是否存在
//        if (StringUtils.isNotEmpty(tbMessageVo.getHeadline())){
//            queryWrapper.like(TbHouseMessage::getHeadline,tbMessageVo.getHeadline());
//        }
//        //判断状态是否存在
//        if (tbMessageVo.getStatus() != null){
//            queryWrapper.eq(TbHouseMessage::getStatus,tbMessageVo.getStatus());
//        }
        //创建分页
        Page<TbHouseMessage> messagePage = null;
        //获取初始页
        Integer pageNum = tbMessageVo.getPageNum();
        //获取分页页数
        Integer pageSize = tbMessageVo.getPageSize();
        //判断是否存在
        if (pageNum != null && pageSize != null){
            //存在则提交分页
            messagePage = new Page<>(pageNum,pageSize);
        }
        //查询分页的对象信息
        Page<TbHouseMessage> page = tbHouseMessageMapper.pageList(messagePage,tbMessageVo.getHeadline(),tbMessageVo.getStatus());
        return ResultVo.SUCCESS(page);
    }

    @Override
    public ResultVo titleById(IdVo id) {
        //根据Vo中的id查询所有信息
        TbHouseMessage selectById = tbHouseMessageMapper.selectById(id.getId());
        //获取访问次数
        Integer pageView = selectById.getPageView();
        //创建实体类对象
        TbHouseMessage thm = new TbHouseMessage();
        //判断访问次数是否存在
        if (pageView!=null){
            //进行访问次数+1
            pageView = pageView +1;
            //获取对象ID
            thm.setId(selectById.getId());
            //获取更新后的次数信息
            thm.setPageView(pageView);
            //修改信息
            tbHouseMessageMapper.updateById(thm);
        }
        //查询更新后的所有信息
        TbHouseMessage tbHouseMessage = tbHouseMessageMapper.selectById(id);
        return ResultVo.SUCCESS(tbHouseMessage);
    }

    @Override
    public ResultVo updateByStatus(TbMessageVo tbMessageVo) {
        //创建实体类
        TbHouseMessage tbHouseMessage = new TbHouseMessage();
        //获取状态信息
        Integer status = tbMessageVo.getStatus();
        //获取id
        tbMessageVo.setId(tbMessageVo.getId());
        //获取状态
        tbMessageVo.setStatus(status);
        //将VO拷贝到实体类中
        BeanUtils.copyProperties(tbMessageVo,tbHouseMessage);
        //修改数据库状态
        tbHouseMessageMapper.updateById(tbHouseMessage);
        return ResultVo.SUCCESS();
    }


}




